% parse notefiles for protocols
% P. manis, 10/28/99
% A completely new version...
%
% The return is a structure of the following form:
%
% 		protocol -> protocol is an array structure itself, as follows:
%		protocol(n).proto	the name of the protocol
%		protocol(n).seq		the sequence that was to be run
%		protocol(n).mode
%		protocol(n).frec		the first record in the protocol
%		protocol(n).lrec		the last record in the protocol
%		protocol(n).ztime		the starting time of the protocol in ztime ( set to 0 here; can be set later when data file is read)
%		protocol(n).etime		the starting time of the protocol in "real time" (read from notefile)
%		protocol(n).valve		The valve setting (initialized at 1)
%		protocol(n).notes		the concatenation of note entries since the last protocol
%	
function [protocol, err] = note_read(filename)
protocol = [];

		protocol.proto=[];		%the name of the protocol
		protocol.seq=[];		%the sequence that was to be run
		protocol.mode=[];		% the type of data acquisition that was done
		protocol.ngrp=[];		% the number of records in each "group" (data, take)
		protocol.frec=[];		%the first record in the protocol
		protocol.lrec=[];		%the last record in the protocol
		protocol.ztime=[];		%the starting time of the protocol in ztime ( set to 0 here; can be set later when data file is read)
		protocol.etime=[];		%the starting time of the protocol in "real time" (read from notefile)
		protocol.valve=[];		%The valve setting (initialized at 1)
		protocol.notes=[];		%the concatenation of note entries since the last protocol

err = 0;
[p f e] = fileparts(filename);

if(strcmp(e, '.mat'))
   [protocol, err] = get_protocol(filename);
   return;
end;

if(strcmp(e, '.abf'))
   dfile = datac('getdfile');
   pindx = 1;
   protocol(pindx).proto = 'gapfree';
   protocol(pindx).seq = '0';
   protocol(pindx).frec = dfile.record;
   protocol(pindx).lrec = dfile.records_in_file; % find the last record
   protocol(pindx).etime = 0; % find the first time associated with block
      protocol(pindx).ztime = 0;
      protocol(pindx).valve = 1;
   protocol(pindx).mode = dfile.dmode;
      protocol(pindx).ngrp = 1;
      err = 0;
      return;
   end;
   
yr = str2num(f(6:7));
%if(yr < 94 & yr > 80)
%	a = note_rdscm2(filename, 1); % this is just a wrapper for scott's version, which seems to work better..
%else
%	a = note_rdscm1(filename);
%end;

	[a, n] = datac_notefile(filename);
	
k = 1;
for i=1:length(a)
	protocol(i).proto = a(i).stim_file;
	protocol(i).seq = a(i).pri_seq;
	protocol(i).mode = a(i).acq_mode;
	protocol(i).ngrp = a(i).take_cnt;
	protocol(i).frec = a(i).beg_rec;
	protocol(i).lrec = a(i).end_rec;
	protocol(i).ztime = 0;
	protocol(i).etime = a(i).time;
	protocol(i).valve = a(i).valve;
	protocol(i).notes = [];
	for j=1:length(n) % check through all the notes 
		if(a(i).beg_rec == n(j).record)
			protocol(i).notes = [protocol(i).notes n(j).note];
		end;
	end
end;

return;
